package com.yangxiangrice.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxiangrice.pojo.*;
import com.yangxiangrice.service.BasListService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @author: shixiaojian
 * @date: 2021年11月18日 23:00
 */
@Controller
@RequestMapping("/baslist")
@CrossOrigin
@Slf4j
public class BasListController {
    @Autowired
    BasListService basListService;

    @GetMapping(value = "/listpage")
    @ResponseBody
    public Result listpage(@RequestParam("page") int page,
                           @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        Subject subject = SecurityUtils.getSubject();
        String phone = subject.getPrincipal().toString();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", phone);
        IPage<BasList> basListIPage = basListService.seletePage(new Page<>(page, limit), wrapper);
        return result.ok(basListIPage);
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public Result save(@RequestBody BasList basList) {
        Result<Boolean> result = new Result<>();
        System.out.println(basList);
        if (ObjectUtil.isEmpty(basList.getBasId())) {
            /*Subject subject = SecurityUtils.getSubject();
            String phone = subject.getPrincipal().toString();
            basList.setPhone(phone);*/
            basList.setCreatetime(new Timestamp(System.currentTimeMillis()));
            boolean tag = basListService.save(basList);
            if (tag) {
                result.ok(tag);
            }else {
                result.fail("添加失败，请重试");
            }
        }else{
            basList.setCreatetime(new Timestamp(System.currentTimeMillis()));
            boolean tag = basListService.updateById(basList);
            if (tag) {
                result.ok(tag);
            }else{
                result.fail("更新失败");
            }
        }
        return result;
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam(value = "basId") Integer basId) {
        Result<Boolean> result = new Result<>();
        boolean tag = basListService.removeById(basId);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @PostMapping(value = "/deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody int[] ids) {
        ArrayList<Integer> list = new ArrayList<>();
        Result<Boolean> result = new Result<>();
        for (int id : ids) {
            list.add(id);
        }
        boolean tag = basListService.removeByIds(list);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @RequestMapping(value = "/search")
    @ResponseBody
    public Result search(@RequestParam("input") String input,
                         @RequestParam("page") int page,
                         @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("basName", input);
        IPage<BasList> basListIPage = basListService.seletePage(new Page<>(page, limit), wrapper);
        if (ObjectUtil.isEmpty(basListIPage.getRecords())) {
            result.fail("未找到符合条件的数据");
        } else {
            result.ok(basListIPage);
        }
        return result;
    }

    @GetMapping(value = "/getplatedata")
    @ResponseBody
    public Result getplatedata() {
        Result<List> result = new Result<>();
        Subject subject = SecurityUtils.getSubject();
        String phone = subject.getPrincipal().toString();
        ArrayList<EchartData> res = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("DISTINCT basName,seedingPlate,freePlateNu");
        wrapper.eq("phone", phone);
        List<BasList> list = basListService.list(wrapper);
        EchartData data = null;
        for (BasList basList : list) {
            data = new EchartData();
            String basName = basList.getBasName();
            data.setName(basName);
            data.setPurchase(basList.getSeedingPlate());
            data.setConsume(basList.getSeedingPlate() - basList.getFreePlateNu());
            res.add(data);
        }
        result.ok(res);
        return result;
    }

    @GetMapping(value = "/getadminplatedata")
    @ResponseBody
    public Result getadminplatedata() {
        Result<List> result = new Result<>();
        ArrayList<EchartData> res = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("DISTINCT basName,seedingPlate,freePlateNu");

        List<BasList> list = basListService.list(wrapper);
        EchartData data = null;
        for (BasList basList : list) {
            data = new EchartData();
            String basName = basList.getBasName();
            data.setName(basName);
            data.setPurchase(basList.getSeedingPlate());
            data.setConsume(basList.getSeedingPlate() - basList.getFreePlateNu());
            res.add(data);
        }
        result.ok(res);
        return result;
    }
    @GetMapping(value = "/getfreeplate")
    @ResponseBody
    public Result getfreeplate(@RequestParam(value ="basId")Integer basId){
        Result<Integer> result = new Result<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("basId",basId);
        BasList basList = basListService.getOne(wrapper);
        if (!ObjectUtil.isEmpty(basList.getFreePlateNu()) && basList.getFreePlateNu() > 0) {
            return result.ok(basList.getFreePlateNu());
        } else {
            result.fail("该基地可用秧盘不足");
        }
        return result;
    }
}
